import java.nio.charset.StandardCharsets;
import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        // Java字符串String是不可变对象；
        // 字符串操作不改变原字符串内容，而是返回新字符串；
        String s = "Hello!";
        // toUpperCase/toLowerCase: 转换大小写
        s = s.toUpperCase();
        System.out.println(s);
        // 字符串在String内部是通过一个char[]数组表示
        String s1 = new String(new char[] { 'H', 'e', 'l', 'l', 'o', '!' });
        System.out.println(s1);
        // contains是否包含子串:
        System.out.println("String contains: " + s1.contains("ll"));
        // indexOf返回子串的起始索引,找不到返回-1
        System.out.println("Index of ll: " + s1.indexOf("ll"));
        // 截取子串:
        System.out.println("Substring from index 2 to 4: " + s1.substring(2, 4));
        // startsWith/endsWith测试前缀/后缀:
        System.out.println("Starts with He: " + s1.startsWith("He"));
        System.out.println("Ends with !: " + s1.endsWith("!"));
        // trim()方法可以移除字符串首尾空白字符。空白字符包括空格，\t，\r，\n
        String s2 = "   Hello!   \n";
        System.out.println("Trimmed string: '" + s2.trim() + "'");
        // strip()方法也可以移除字符串首尾空白字符。它和trim()不同的是，类似中文的空格字符\u3000也会被移除
        String s3 = "\u3000\u3000Hello!\u3000\u3000\n";
        System.out.println("Stripped string: '" + s3.strip() + "'");
        // isEmpty()和isBlank()来判断字符串是否为空和空白字符串
        String s4 = "   \n";
        System.out.println("Is empty: " + s4.isEmpty());
        System.out.println("Is blank: " + s4.isBlank());
        // replace: 替换字符串
        String s5 = "Hello, World!";
        System.out.println("Replace World with Java: " + s5.replace("World", "Java"));
        // replaceAll： 替换所有符合正则表达式的子串
        String s6 = "a1b2c3d4";
        System.out.println("Replace all digits with #: " + s6.replaceAll("\\d", "#"));
        // split(): 按照正则表达式拆分字符串
        String s7 = "apple,banana;orange grape";
        String[] fruits = s7.split("[,; ]+");
        System.out.println("Split fruits:" + Arrays.toString(fruits));
        // join(): 将多个字符串连接成一个字符串
        String joined = String.join(" | ", fruits);
        System.out.println("Joined fruits: " + joined);
        // formatted()方法和format()静态方法
        // %s：显示字符串；
        // %d：显示整数；
        // %x：显示十六进制整数；
        // %f：显示浮点数
        String name = "Alice";
        int age = 30;
        String formatted1 = "Name: %s, Age: %d".formatted(name, age);
        String formatted2 = String.format("Name: %s, Age: %d", name, age);
        System.out.println("Formatted string 1: " + formatted1);
        System.out.println("Formatted string 2: " + formatted2);

        // 类型转换
        // valueOf(): 将其他类型转换为字符串
        System.out.println("String value of num: " + String.valueOf(123));
        System.out.println("String value of boolean: " + String.valueOf(true));
        // parseInt()/parseDouble(): 将字符串转换为对应的数值类型
        System.out.println("Parsed int: " + Integer.parseInt("456"));
        System.out.println("Parsed double: " + Double.parseDouble("3.14"));
        // parseBoolean(): 将字符串转换为布尔类型
        System.out.println("Parsed boolean (true): " + Boolean.parseBoolean("true"));
        System.out.println("Parsed boolean (false): " + Boolean.parseBoolean("False"));
        // getInteger(): 将该字符串对应的系统变量转换为Integer
        System.out.println("Get Integer object: " + Integer.getInteger("java.version"));

        // toCharArray(): 将字符串转换为字符数组
        char[] charArray = "Hello".toCharArray();
        System.out.println("Character array: " + Arrays.toString(charArray));
        System.out.println("String from char array: " + new String(charArray));

        // getBytes(): 将字符串转换为字节数组指定UTF_8编码
        byte[] byteArray = "Hello".getBytes(StandardCharsets.UTF_8);
        System.out.println("Byte array: " + Arrays.toString(byteArray));
    }
}
